package cn.ywang.spring.framework.web.servlet;

import cn.ywang.spring.utils.StringUtils;

import java.io.File;
import java.util.Locale;

/**
 * @author BeatificWang
 * @date 2019/5/3 19:10
 */
public class ViewResolver {

	private File templateDir;

	private String DEFAULT_SUFFIX_NAME = ".html";

	public ViewResolver(File templateFile) {
		this.templateDir = templateFile;
	}

	public View resolveViewName(String viewName, Locale locale) {
		if (StringUtils.isBlank(viewName)) {
			return null;
		}
		viewName = viewName.endsWith(this.DEFAULT_SUFFIX_NAME) ? viewName : viewName + this.DEFAULT_SUFFIX_NAME;
		File file = new File((this.templateDir.getParent() + "/" + viewName).replaceAll("/+", "/"));
		return new View(file);
	}
}
